package sqlexplorer.commands;

import oracle.net.ns.ConnectPacket;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.PlatformUI;

import sqlexplorer.application.ConnectionsController;
import sqlexplorer.domain.DatabaseConnection;
import sqlexplorer.ui.views.ConnectionsView;
import sqlexplorer.ui.views.CreateConnectionDialog;
import sqlexplorer.ui.views.EditConnectionDialog;

public class EditContextHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		
		IViewReference [] iviews = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
		for(IViewReference viewReference : iviews) {
			if(viewReference.getId().equals("sqlexplorer.ui.views.connectionsview")) {
				ConnectionsView connectionView = (ConnectionsView)viewReference.getView(true);
				
				ISelection selection = connectionView.getViewer().getSelection();
				
				if(selection != null) {
					IStructuredSelection structureSelection = (IStructuredSelection)selection;
					int index = ConnectionsController.getInstance().getIndexForName((String)structureSelection.getFirstElement());
					
					Shell shell = viewReference.getView(true).getSite().getShell();
					EditConnectionDialog dialog = new EditConnectionDialog(shell, index);
					
					/*
					DatabaseConnection databaseconnection = ConnectionsController.getInstance().getDatabaseConnection(index);
					
					dialog.setConnectionName(databaseconnection.getConnectionName());
					dialog.setSelectedDatabase(databaseconnection.getDatabaseType());
					dialog.setURL(databaseconnection.getJdbcUrl());
					dialog.setUser(databaseconnection.getUserName());
					dialog.setPassword(databaseconnection.getPassword());
					*/
					dialog.open();
				}
			}
		}
		
		return null;
	}

}
